// 时间戳转日期格式 
export function timestampToTime(value, type = "date", symbol = "-") {

	var time = new Date(value);
	var year = time.getFullYear();
	var month = time.getMonth() + 1;
	var date = time.getDate();
	var hour = time.getHours();
	var minute = time.getMinutes();
	var second = time.getSeconds();
	month = month < 10 ? "0" + month : month;
	date = date < 10 ? "0" + date : date;
	hour = hour < 10 ? "0" + hour : hour;
	minute = minute < 10 ? "0" + minute : minute;
	second = second < 10 ? "0" + second : second;

	// 2023-02-02 12:12:12
	if (type === 'time') {
		return year + symbol + month + symbol + date + " " + hour + ":" + minute + ":" + second;
	}

	// 2023-02-02
	else if (type === 'date') {
		return year + symbol + month + symbol + date;
	}
	// 2023-02
	else if (type === 'year-month') {
		return year + symbol + month;
	}
	// 2023
	else if (type === 'year') {
		return year;
	}
	else {
		return ""
	}
}

// 节流函数的封装
export function throttle(fn, interval) {
	let lastCall = 0;
	return function (...args) {
		const now = Date.now();
		if (now - lastCall >= interval) {
			lastCall = now;
			fn.apply(this, args); // 每隔 interval 时间执行一次
		}
	};
}

// 防抖函数的封装
export function debounce(fn, delay) {
	let timer = null;
	return function (...args) {
		if (timer) clearTimeout(timer);  // 每次触发事件时，清除上一次的定时器
		timer = setTimeout(() => {
			fn.apply(this, args); // 一段时间后才执行
		}, delay);
	};
}

/* 根据后缀判断文件类型 */
export function getFileType(fileName) {
	let suffix = ''; // 后缀获取
	let result = ''; // 获取类型结果
	if (fileName) {
		const fileArr = fileName.split('.'); // 根据.分割数组
		suffix = fileArr[fileArr.length - 1]; // 取最后一个
	}
	if (!suffix) return false; // fileName无后缀返回false
	suffix = suffix.toLocaleLowerCase(); // 将后缀所有字母改为小写方便操作
	// 匹配图片
	const imgList = ['png', 'jpg', 'jpeg', 'bmp', 'gif']; // 图片格式
	result = imgList.find(item => item === suffix);
	if (result) return 'image';
	// 匹配txt
	const txtList = ['txt'];
	result = txtList.find(item => item === suffix);
	if (result) return 'txt';
	// 匹配excel
	const excelList = ['xls', 'xlsx'];
	result = excelList.find(item => item === suffix);
	if (result) return 'excel';
	// 匹配word
	const wordList = ['doc', 'docx'];
	result = wordList.find(item => item === suffix);
	if (result) return 'word';
	// 匹配pdf
	const pdfList = ['pdf'];
	result = pdfList.find(item => item === suffix);
	if (result) return 'pdf';
	// 匹配ppt
	const pptList = ['ppt', 'pptx'];
	result = pptList.find(item => item === suffix);
	if (result) return 'ppt';
	// 匹配zip
	const zipList = ['rar', 'zip', '7z'];
	result = zipList.find(item => item === suffix);
	if (result) return 'zip';
	// 匹配视频
	const videoList = ['mp4', 'm2v', 'mkv', 'rmvb', 'wmv', 'avi', 'flv', 'mov', 'm4v'];
	result = videoList.find(item => item === suffix);
	if (result) return 'video';
	// 匹配音频
	const radioList = ['mp3', 'wav', 'wmv'];
	result = radioList.find(item => item === suffix);
	if (result) return 'radio';
	// 其他文件类型
	return 'other';
}

export function getPermissions() {
	return wx.getStorageSync('permissions')
}
